<?php

namespace app\index\controller;

use app\index\model\Coursetype;
use app\index\model\Filess;
use app\index\model\Jstype;
use app\index\model\Video;
use app\index\controller\Base;
use app\index\model\Course;
use app\index\model\Apply;
use app\index\model\Classtype;
use app\index\model\Myclass;
use think\Request;
use think\Session;

class  Courses extends Base
{
    public function index()
    {
        $user = $this->user_info();
        //查询当前用户的报名信息
        $Applys = Apply::where(array('idcard' => $user['idcard']))->select();

        //查到课程
        $two_id = '';
        $courseid = array();
        foreach ($Applys as $v) {
            array_push($courseid, $v['courseid']);

        }
        // 查询课程分类
        $Course = Course::where(array('id' => ['in', implode(",", $courseid)]))->select();
        $Jstypeid =array();
        foreach ($Course as $vv) {
            if($vv['type'] == 7){
                $two_id.=$vv['type'].',';
            }else{
                array_push($courseid, $vv['type']);
            }
        }


        $Jstype = Jstype::where(array('id' => ['in', implode(",", $courseid)]))->where('id!=7')->select();
        $two_jstype = Course::where(array('id'=>['in',trim($two_id,',')]))->select();
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "Jstype" => $Jstype,
            'two_jstype'=>$two_jstype
        );
        return view("index", $data);

    }

    public function coursetypes(Request $request)
    {
        $list = Coursetype::where(array('type' => $request->get('id')))->select();
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => $list,
        );

        return view("coursetypes", $data);
    }

    public function coursef(Request $request)
    {
        $user = $this->user_info();
        //查询当前用户的报名信息
        $Applys = Apply::where(array('idcard' => $user['idcard']))->select();
        $courseid = array();
        foreach ($Applys as $v) {
            array_push($courseid, $v['courseid']);

        }
        $list = Course::where(array('type' => $request->get('id')))->where(array('id' => ['in', implode(",", $courseid)]))->select();
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => $list,
        );
        return view("coursef", $data);
    }

    public function coursefs(Request $request)
    {
        $list = Course::where(array('type' => $request->get('id')))->select();
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "list" => $list,
        );
        return view("coursef", $data);
    }

    public function videoandfile(Request $request)
    {
        if (Session::has('user_infos')) {
            $user = Session::get("user_infos");
        } else {
            $user = '';
        }
        $video = Video::where(array('type' => $request->get('id')))->select();
        $files = Filess::where(array('type' => $request->get('id')))->select();
        foreach ($video as  $vk=> $vv) {
            $video[$vk]['url'] = str_replace("\\", "/", $vv['url']);;
        }
        foreach ($files as  $fk=>$fv ) {
            $files[$fk]['url']= str_replace("\\", "/", $fv['url']);;
        }

        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "video" => $video,
            "files" => $files,
            "user" => $user,
        );
        return view("videoandfile", $data);
    }


    /*
     * 网络课程列表
     * */
    public function NetworkConstant()
    {
        $Jstype = Jstype::where('id!=7')->select();
        // 查询二级的分类进行前台显示
        $two_jstype = Course::where('type=7')->select();
        //查询热门推荐信息
        $data = array(
            "setting" => $this->getAllSetting(),
            "Jstype" => $Jstype,
            "two_jstype" => $two_jstype,
        );
        return view("NetworkConstant", $data);
    }

    /*
     * 验证用户是否已经报名本课程
     * */
    public function isCurriculum()
    {
        $user = $this->user_info();
        // 查询该资料的id
        $id = $_POST['id'];
        // 查询该用户报名的所有班级
        $classid_arr = Apply::field('classid')->where('idcard="' . $user['idcard'].'"')->select();
        // 查询该用户已入班级
        $classType = '';
        foreach($classid_arr as $k=>$v){

            // 查询入班的班级类型
            if($v['classid'] !=''){
                $class_type = Myclass::field('type')->where('classid="'.$v['classid'].'"')->find();
                $classType .= $class_type['type'].',';

            }

        }
        // 查询所有的班级类型
        if($classType!=''){
            $Class_arr = Classtype::field('course')->where('id in ('.trim($classType,',').')')->select();
            $course_str = '';
            foreach($Class_arr as $kk=>$vv){
                $course_str .= $vv['course'].',';
            }

            // 判断班级类型的所有班级是否包含本课程
            if($course_str!=''){
                $course_arr = explode(',',$course_str);
                if(in_array($id,$course_arr)){
                    $res = 1;
                }else{
                    $res = 0;
                }
            }else{
                $res = 0;
            }

        }else{
            $res = 0;
        }

        if (!empty($res)) {
            return 1;
        } else {
            return 0;
        }
    }
}
